<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<div class="row" style="margin:0;padding:0;"> 
		<div class="col-xs-2" style="margin:0px auto;padding:0px;">
					 <div class="widget-box"  style="margin:0 auto;">
												<div class="widget-header widget-header-flat">
													<b>标签树</b>
												</div> 
												<div class="widget-body"> 
													<div style="padding:4px;">
													 	<button id="id-btn-dialogk" class="btn btn-sm btn-primary">增加</button>
													 	<button id="id-btn-dialogc" class="btn btn-sm btn-primary">编辑</button>
													 	<button id="id-btn-dialogd" class="btn btn-sm btn-danger">删除</button>
													 </div>
												 	<ul id="treeDemo1" class="ztree"></ul>
												</div>
					 </div>
			</div>
			<div class="col-xs-10"  id="class_mainContent" style="margin:0px auto;padding:0px;padding-left:4px;">
					<div class="widget-box" style="margin:0 auto;">
						<div class="widget-header widget-header-flat">
							<b>标签详细信息</b>
						</div>
						<div class="widget-body" style="min-height:600ox;"> 
							
						 	<div class="" style="padding: 5px;">  
								<div style="float:right;">
									<input type="text" id="serch_text" style="width:250px" placeholder="请输入标签名称">
								</div>
							</div>
							<div style="padding:5px;">
								 <table class="table table-striped table-hover table-bordered">
								  <thead> 
										<tr id="sys_Lable_table_title">
											  <th  width="40">编号</th>
											  <th  width="100" fname="name">标签名称</th>
											  <th  width="30" fname="lableValue">分值</th>
											  <th  width="60" fname="type">类型</th>
											  <th  width="100" fname="jiaocai">所属教材</th>
											  <th  width="40" fname="lableImportance">重要性</th> 
											  <th  width="100" fname="string1">所属用户</th>   
											  <th  width="100" fname="showTime">创建时间</th> 
										 </tr>
								 	 </thead>
									  <tbody id="sys_Lable_table">
										 
									  </tbody>
									</table> 
									<ul class="pagination" id="Lable_pagination">
									  
									</ul> 
							
							</div>		
					 
						</div>
					</div> 
			</div>  
</div> 
<script>
(function(){
	var setting = {	
			view: {
				dblClickExpand: false,
				showLine: true,
				selectedMulti: false
			},callback: {
				onClick: zTreeOnClick
			},
			data: { 
			simpleData: {
				enable:true,
				idKey: "lableId",
				pIdKey: "lablePid", 
				rootPId: "0"
			}
		}};
		//点击事件
		function zTreeOnClick(event, treeId, treeNode) { 	
			 
		};
	   function loadTree(){
		    var url ="${pageContext.request.contextPath}/edu/sys_LableAction.do?method=getAllLableTree";
			$.ajax({
			   type: "POST",
			   url: url,
			   data:{type:0},
			   dataType:"json",
			   success: function(msg){  
				   $.fn.zTree.init($("#treeDemo1"), setting, msg[0].list);
			   }
			}); 
	 }
	function addClass(){
		//增加班
		$("#id-btn-dialogk").click(function(){
				 if("${sessionScope.sysUser.userType}"=="6" || "${sessionScope.sysUser.userType}"=="4"){
					alert("你无权增加标签");
					return;
				}
				var treeObj = $.fn.zTree.getZTreeObj("treeDemo1");
				var nodes = treeObj.getSelectedNodes(); 
				var node={};
				if(nodes!=null && nodes.length>0){
					node=nodes[0];
				}
			 	$("#user_include").html("");
			 	$("#user_include").load("${pageContext.request.contextPath}/lableAdd.jsp?r="+Math.random(),node,function(){
			 		 
			 		var dialog = $("#dialog-message").removeClass("hide").dialog({
						modal: true,
						title: "增加教学标签",
						width:800,
						title_html: false,
						close:function(){
							$(this).dialog('destroy'); 
							$("#user_include").html("");
							//loadTree();
						},
						buttons: [  
							{
								text: "提交",
								"class" : "btn btn-primary btn-xs",
								click: function() { 
									window.lableAdd.addLable(this);
								} 
							}
						]
					}); 
				}); 
		}); 
		//删除
		$("#id-btn-dialogd").click(function(){
			var r=confirm("删除节点，你确认要删除这个节点吗?");
			  if (r==true){
				    var treeObj = $.fn.zTree.getZTreeObj("treeDemo1");
					var nodes = treeObj.getSelectedNodes();
					if(nodes!=null && nodes.length>0){
						$.ajax({
							   type: "POST",
							   async:false,
							   url: "${pageContext.request.contextPath}/edu/sys_LableAction.do?method=deleteLable&r="+Math.random(),
							   data: {table_id:nodes[0].table_id,pName:nodes[0].pName},
							   dataType:"json",
							   success: function(msg){ 
							      if(msg.code==1){
							    	  alert(msg.message);
							    	  loadLable(null,{});
							    	  loadTree();
							      }else{
							    	  alert(msg.message);
							      }
							   }
						});
					}else{
						alert("未选中节点");
					}
					
			   }
			  
		});
		//修改
		$("#id-btn-dialogc").click(function(){
				if("${sessionScope.sysUser.userType}"=="6" || "${sessionScope.sysUser.userType}"=="4"){
					alert("你无权修改标签!");
					return;
				}
				var treeObj = $.fn.zTree.getZTreeObj("treeDemo1");
				var nodes = treeObj.getSelectedNodes();
				if(nodes!=null && nodes.length>0){ 
					
					var pName ="";
					var node = nodes[0].getParentNode();
					 if(node!=null){
						 pName = node.name;
						 nodes[0].pName=pName;
					 }
					$("#user_include").html("");
				 	$("#user_include").load("${pageContext.request.contextPath}/lableUpdate.jsp?r="+Math.random(),nodes[0],function(){				 		 
				 		var dialog = $("#dialog-message").removeClass("hide").dialog({
							modal: true,
							title: "修改标签",
							width:800,
							title_html: false,
							close:function(){
								$(this).dialog('destroy'); 
								$("#user_include").html("");
								//loadTree();
							},
							buttons: [  
								{
									text: "提交",
									"class" : "btn btn-primary btn-xs",
									click: function() {
										window.lableUpdate.updateLable(this);
									} 
								}
							]
						}); 
					}); 
				}else{
					alert("未选中节点！");
				}
		});
	};
	//查询所有的标签
	function loadLable(event,data){
		if(typeof(data)=="undefined"){
			data={};
		}
		var url ="${pageContext.request.contextPath}/edu/sys_LableAction.do?method=getAllLable";
		var cp =$("#Lable_pagination").find(".active");
		data.start=1;
		data.name=$("#serch_text").val();
		if(cp.length>0){
			var ccp=$(cp[0]).text();
			if(parseInt(ccp)){
				data.start=ccp;
			}
		}  
		$("#table_Lable").wgrid({
					table_title:"sys_Lable_table_title",
					tbodyId:"sys_Lable_table",
					pagination:"Lable_pagination",
					url:url,
					//checkbox:true,
					data:data,
					success:function(){
						changeLable();
					}
		});
	};
	//改变头像 和性别，还有类型
	function changeLable(){ 
		var allstate =$("div[name='sys_Lable_table_lableImportance_tr_td']");
		 
		for(var i=0;i<allstate.length;i++){
			var bc = $(allstate[i]);
			if(bc.text()==1){
				bc.html('<font color="red">高</font>');
			}else if(bc.text()==2){
				bc.html('<font color="green">中</font>');
			}else{
				bc.html('<font color="green">低</font>');
			}
		}
		var allstate =$("div[name='sys_Lable_table_type_tr_td']");
		for(var i=0;i<allstate.length;i++){
			var bc = $(allstate[i]);
			if(bc.text()==2){
				bc.html('<font color="green">老师标签</font>');
			}else if(bc.text()==5){
				bc.html('<font color="red">专家标签</font>');
			}
		}
	}
	//搜索的回车事件
	$('#serch_text').keydown(function(e){
		if(e.keyCode==13){
			loadLable(null,{});
		}
	});
	$(function(){
		addClass();
		loadTree();
		loadLable(null,{});
	});
	window.lable={};
	window.lable.loadTree=loadTree;
	window.lable.loadLable=loadLable;
})();
</script>